package com.zzy.demo.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.zzy.cms.core.base.service.BaseServiceImpl;
import com.zzy.demo.dao.PictureDao;
import com.zzy.demo.model.Picture;


@Service
public class PictureService extends BaseServiceImpl<PictureDao, Picture, String>{
	
	@Autowired
	PictureDao pictureDao;
	
	
	public Picture findByStart(Long start) {
		String sql ="select * from t_picture order by cteate_time desc limit :start, 1 ";
		Map<String, Object> params = Maps.newHashMap();
		params.put("start",start);
		List<Picture> list =pictureDao.findSql(sql, Picture.class , params);
		if(null!=list && !list.isEmpty()) {
			Picture p = list.get(0);
			return p;
		}
		return null;
	}

	public Page<Picture> findByPage(Pageable pageable) {
		String sql ="select * from t_picture order by cteate_time desc ";
		Page<Picture> page =pictureDao.findPageSql(sql, Picture.class, pageable, Maps.newHashMap());
		return page;
	}
	
	
	public Integer deleteByFileKey(String fileKey) {
		String sql ="delete from t_picture where file_key = :fileKey ";
		Map<String, Object> params = Maps.newHashMap();
		params.put("fileKey",fileKey);
		return pictureDao.executeSql(sql, params);
	}
	
    public Integer updateByFileKey(String fileKey, String title, String tags) {
    	List<Picture> list = pictureDao.findByFileKey(fileKey);
    	if(null!=list && !list.isEmpty()) {
        	Picture picture =list.get(0);
        	picture.setTags(tags);
        	picture.setTitle(title);
        	Picture dbPicture = pictureDao.save(picture);
        	if(null!=dbPicture) {
              	return 1;
        	}
    	}
		return 0;
    }

}
